package com.senon.mvpretrofitrx.mvp.activity;

import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.senon.mvpretrofitrx.R;
import com.senon.mvpretrofitrx.mvp.base.BaseActivity;
import com.senon.mvpretrofitrx.mvp.contract.NewsContract;
import com.senon.mvpretrofitrx.mvp.presenter.NewsPresenter;
import com.senon.mvpretrofitrx.mvp.test.BaseResponse;
import com.senon.mvpretrofitrx.mvp.test.NewsResult;
import com.senon.mvpretrofitrx.mvp.utils.ToastUtil;

import java.util.HashMap;

import butterknife.BindView;
import io.reactivex.ObservableTransformer;

public class TestActivity extends BaseActivity<NewsContract.NewsView, NewsContract.Presenter> implements NewsContract.NewsView {

    @BindView(R.id.btn_get_news)
    Button btnTest;

    @Override
    public int getLayoutId() {
        return R.layout.activity_test;
    }

    @Override
    public NewsContract.Presenter createPresenter() {
        return new NewsPresenter(this);
    }

    @Override
    public NewsContract.NewsView createView() {
        return this;
    }

    @Override
    public void init() {
        btnTest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HashMap<String, String> params = new HashMap<>();
                params.put("key", "d600a9f93f8ab5c302a632647f1788e4");
                params.put("type", "top");
                getPresenter().getNews(params, true, false);
            }
        });
    }

    /**
     * NewContract.View
     *
     * @param data
     */
    @Override
    public void result(BaseResponse<NewsResult> data) {
        Log.i("TAG", "data:" + data);
    }

    @Override
    public void setMsg(String msg) {
        ToastUtil.showShortToast(msg);
    }

    @Override
    public <T> ObservableTransformer<T, T> bindLifecycle() {
        //        return this.bindUntilEvent(ActivityEvent.PAUSE);//绑定到Activity的pause生命周期（在pause销毁请求）
        return this.bindToLifecycle(); //绑定activity，与activity生命周期一样
    }
}
